/*
  Analog input, analog output, serial output

  Reads an analog input pin, maps the result to a range from 0 to
  65535 and uses the result to set the pulse width modulation (PWM) of
  an output pin.  Also prints the results to the serial monitor.

  (You may need to change the pin numbers analogInPin and analogOutPin
  if you're not using a Maple).

  The circuit:
  * Potentiometer connected to analog pin 15.
    Center pin of the potentiometer goes to the analog pin.
    Side pins of the potentiometer go to +3.3V and ground.
  * LED connected from digital pin 9 to ground

  created 29 Dec. 2008
  by Tom Igoe

  ported to Maple
  by LeafLabs
*/

// These constants won't change.  They're used to give names
// to the pins used:
#include "config.h"
#define DISABLE_ACK  //定义禁止发送应答

#define LED4 43

#define PWM1_ID 5
#define PWM2_ID 9
#define PWM3_ID 14
#define PWM4_ID 24
#define PWM5_ID 6
#define PWM6_ID 7
#define PWM7_ID 8
#define PWM8_ID 3
HardwareSPI SPI(2);
static RadioSi446x radio;

static u32 currentTime = 0;
static u32 previousTime = 0;
u8 PWM_PIN[8] = {PWM1_ID,PWM2_ID,PWM3_ID,PWM4_ID,PWM5_ID,PWM6_ID,PWM7_ID,PWM8_ID};
u16 pwmVal[8];
void initPWMout(void)
{
	int i;
	for(i=0;i<8;i++)pinMode(PWM_PIN[i],PWM); 
	
	timer_set_mode(TIMER4, TIMER_CH4, TIMER_PWM);
    timer_set_mode(TIMER4, TIMER_CH3, TIMER_PWM);
    timer_set_mode(TIMER4, TIMER_CH2, TIMER_PWM);
    timer_set_mode(TIMER4, TIMER_CH1, TIMER_PWM);
    
    timer_pause(TIMER4);
    Timer4.setPrescaleFactor(72);
    timer_set_count(TIMER4, 0);
    timer_set_reload(TIMER4, 5000);// 60000, 55000, 50000 Period of the PWM signal
                                   // 48000 falls off the cliff w/ Plush 25Amp
                                   // 50000 spotty w/ 1 out of 3 esc's
   timer_resume(TIMER4);           // enable outputs to esc's
    timer_set_mode(TIMER1, TIMER_CH1, TIMER_PWM);
    timer_set_mode(TIMER1, TIMER_CH2, TIMER_PWM);
    timer_set_mode(TIMER1, TIMER_CH3, TIMER_PWM);
    
    timer_pause(TIMER1);
    Timer1.setPrescaleFactor(72);
   timer_set_count(TIMER1, 0);
   timer_set_reload(TIMER1, 5000);// 60000, 55000, 50000 Period of the PWM signal
                                   // 48000 falls off the cliff w/ Plush 25Amp
                                   // 50000 spotty w/ 1 out of 3 esc's
   timer_resume(TIMER1);           // enable outputs to esc's
   
    timer_set_mode(TIMER2, TIMER_CH2, TIMER_PWM);
    timer_pause(TIMER2);
    Timer2.setPrescaleFactor(72);
   timer_set_count(TIMER2, 0);
   timer_set_reload(TIMER2, 5000);// 60000, 55000, 50000 Period of the PWM signal
                                   // 48000 falls off the cliff w/ Plush 25Amp
                                   // 50000 spotty w/ 1 out of 3 esc's
   timer_resume(TIMER2);           // enable outputs to esc's
   for(i=0;i<8;i++)pwmWrite(PWM_PIN[i],1000); 
   
}
void setup() {
    
     pinMode(LED4, OUTPUT);
         
 #if 1
   pinMode(RADIO_SDN_PIN, OUTPUT);    
    // Set up SPI  
  pinMode(SCKpin,  OUTPUT);
  pinMode(SSpin,   OUTPUT);
  pinMode(MOSIpin, OUTPUT);
  
   SPI.begin(SPI_1_125MHZ , MSBFIRST, 0); 
   pinMode(SSpin,   OUTPUT); 
   delay(600);
    radio.vRadio_Init();    
   radio.si446x_set_property(0x12,3,0x08,0x0a,0x01,0x00); 
    radio.vRadio_StartRX_Variable_Packet(pRadioConfiguration->Radio_ChannelNumber); 
  #else
      radio.setup();
      pinMode(SSpin,   OUTPUT);  
  #endif
   initPWMout();
    currentTime = micros();
     previousTime=currentTime;
   // radio.ptt_on(); 
}
int testVal;
u8 radioStat;
u8 m_RFTxTimeOut_ct;
u8 m_toSendTimeOut_ct;
const u8 m_RFAck[]={0x04,'A','C','K'};

#define RF_SEND_MAX_TIME   10


void loop() 
{
  LoopSerialCom();
  currentTime = micros();
  if((currentTime-previousTime)>10000)
  {//10ms delay
  	   
     togglePin(LED4);
      previousTime=currentTime;
     if(m_RFTxTimeOut_ct<RF_SEND_MAX_TIME)m_RFTxTimeOut_ct++;//发送超时 
     if(m_toSendTimeOut_ct) m_toSendTimeOut_ct+=10;//接收超时
  }
  LoopCheckDataRFSend();
  LoopSi446xFun();
  
  
   
  
}
